package com.ossjk.api.controller;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.system.entity.Attachment;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.*;
import com.ossjk.oa.system.vo.EmpVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Api(description = "入职登记")
@RestController
@RequestMapping("/api/entry")
public class EntryController extends BaseController {
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private IEmployeePostService iEmployeePostService;
    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IPostService iPostService;
    @Autowired
    private IAttachmentService iAttachmentService;
    @Value("${files.path}")
    private String filePath;


    /*
     * 显示部门
     *
     * */
    @ApiOperation(value = "部门列表", notes = "部门列表")
    @GetMapping("/selectDepa")
    public ResponseBean<List<Department>> selectDepartment() {
        Wrapper wrapper = Condition.create();
        List<Department> list = iDepartmentService.selectList(wrapper);
        return (ResponseBean<List<Department>>) responseSuccess(list);
    }

    /*
     *根据部门显示员工
     *
     * */
    @ApiOperation(value = "部门员工列表", notes = "部门员工列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "did", value = "部门id", paramType = "query", dataType = "String",required = true)
    })
    @GetMapping("/selectEmpDepa")
    public ResponseBean<List<EmpVo>> selectEmp(String did) {
        Wrapper wrapper = Condition.create().eq("did", did);
        List<EmpVo> empVos = iEmployeeService.selectEmpVosByDid(did);
        return (ResponseBean<List<EmpVo>>) responseSuccess(empVos);
    }

    /*
     * 查询个人信息
     * */
    @ApiOperation(value = "员工个人信息", notes = "员工个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eid", value = "员工id", paramType = "query", dataType = "String",required = true)
    })
    @GetMapping("/selectEmp")
    public ResponseBean<Employee> selectPersonal(String eid) {
        Wrapper wrapper = Condition.create();
        if (CommonUtil.isBlank(eid)) {
            wrapper.eq("id", getCurrnetUserId());
        } else {
            wrapper.eq("id", eid);
        }
        Employee employee = iEmployeeService.selectOne(wrapper);
        return (ResponseBean<Employee>) responseSuccess(employee);
    }

    /*
     * 添加附件
     * */
    @ApiOperation(value = "上传附件", notes = "上传附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "file", paramType = "query", dataType = "MultipartFile")
    })
    @PostMapping("/uploadEnclosure")
    public ResponseBean uploadEnclosure(MultipartFile file, HttpServletRequest request) throws IOException, ServletException {
        String userId = getCurrnetUserId();
        String fileName = file.getOriginalFilename();
        String newName = new Date().getTime() + fileName.substring(fileName.lastIndexOf("."));
        Attachment attachment = new Attachment();
        filePath += "/j200106/";
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        InputStream inputStream = file.getInputStream();
        OutputStream outputStream = new FileOutputStream(filePath + newName);

        FileCopyUtils.copy(inputStream, outputStream);
        attachment.setFile(newName);
        attachment.setEid(userId);
        attachment.setUrl(filePath);
        attachment.setCrer(userId);
        attachment.setCrtm(new Date());
        attachment.setIsdelete(new BigDecimal(1));
        boolean b = iAttachmentService.insert(attachment);
        if (b) {
            return (ResponseBean) responseSuccess("上传附件成功");
        } else {
            return (ResponseBean) responseFail("上传附件失败");
        }
    }

    @ApiOperation(value = "去修改上传附件", notes = "去修改上传附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "员工id", paramType = "query", dataType = "String")
    })
    @PostMapping("/toUpdateUpLoad")
    public ResponseBean<Attachment> toUpdateUpLoad(String id) {
        if (CommonUtil.isBlank(id)) {
            return (ResponseBean<Attachment>) responseFail("id：不能为空");
        }
        Wrapper wrapper = Condition.create().eq("eid", id);
        Attachment attachment = iAttachmentService.selectOne(wrapper);
        return (ResponseBean<Attachment>) responseSuccess(attachment);
    }

    @ApiOperation(value = "修改附件", notes = "修改附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping("/updateUpLoad")
    public ResponseBean updateUpLoad(MultipartFile file, String id) throws IOException {
        if (CommonUtil.isBlank(id)) {
            return (ResponseBean<Attachment>) responseFail("id：不能为空");
        }
        String currnetUserId = getCurrnetUserId();
        String fileName = file.getOriginalFilename();
        String newName = new Date().getTime() + fileName.substring(fileName.lastIndexOf("."));
        Attachment attachment = new Attachment();
        filePath += "/j200106/";
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        InputStream inputStream = file.getInputStream();
        OutputStream outputStream = new FileOutputStream(filePath + newName);
        FileCopyUtils.copy(inputStream, outputStream);
        attachment.setFile(newName);
        attachment.setEid(currnetUserId);
        attachment.setId(id);
        attachment.setUrl(filePath);
        attachment.setCrer(currnetUserId);
        attachment.setCrtm(new Date());
        boolean b = iAttachmentService.updateById(attachment);
        if (b) {
            return (ResponseBean) responseSuccess("修改附件成功");
        } else {
            return (ResponseBean) responseFail("修改附件失败");
        }
    }

    @ApiOperation(value = "删除附件", notes = "删除附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping("/deleteUpload")
    public ResponseBean deleteUpload(String id) {
        if (CommonUtil.isBlank(id)) {
            return (ResponseBean) responseFail("id：不能为空");
        }
        boolean b = iAttachmentService.deleteById(id);
        if (b) {
            return (ResponseBean) responseSuccess("删除附件成功");
        } else {
            return (ResponseBean) responseFail("删除附件失败");
        }
    }

}
